﻿using System;
using System.Collections.Generic;
using Module.Core.Member;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data;
using Module.Core.BusinessLogic;
using Module.Core.EntitiesInfo;

namespace Web.Test.ConTrols
{
    public partial class MaTrix : System.Web.UI.UserControl
    {
        #region khai báo
        private string userName = string.Empty;
        MemberInfo info = null;
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            userName = MemberBL.GetValue();
            info = MemberBL.SelectProFile(userName);
            TreeMemBer tree = new TreeMemBer();
            String codeMember = "";
            try
            {
                codeMember = Request.QueryString["codeMember"].ToString().Trim();
                drawTree(codeMember);
            }catch(Exception ee){

            }
           
           
        }
        protected void btnTop_Click(object sender, EventArgs e)
        {
            drawTree(btnTop.Text.Trim());
            
            //String url = "MaTrix.aspx?codeMember=" + ;
            //Response.Redirect(url);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            drawTree(Button2.Text.Trim());
            //String url = "MaTrix.aspx?codeMember=" + Button2.Text.Trim();
            //Response.Redirect(url);
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            drawTree(Button3.Text.Trim());
            //String url = "MaTrix.aspx?codeMember=" + Button3.Text.Trim();
            //Response.Redirect(url);
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            drawTree(Button4.Text.Trim());
            //String url = "MaTrix.aspx?codeMember=" + Button4.Text.Trim();
            //Response.Redirect(url);
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            drawTree(Button1.Text.Trim());
            //String url = "MaTrix.aspx?codeMember=" + Button1.Text.Trim();
            //Response.Redirect(url);
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            drawTree(Button5.Text.Trim());
            //String url = "MaTrix.aspx?codeMember=" + Button5.Text.Trim();
            //Response.Redirect(url);
        }
        protected void Button6_Click(object sender, EventArgs e)
        {
            String lastMember = "";
            TreeMemBer tree = new TreeMemBer();
            DataTable dt = tree.getMemberLast(btnTop.Text.Trim());
            if (dt.Rows.Count > 0)
            {
                lastMember = dt.Rows[0]["CodeMember"].ToString().Trim();
                drawTree(lastMember);
                //String url = "MaTrix.aspx?codeMember=" + lastMember;
                //Response.Redirect(url);
            }
        }
        public void drawTree(String codeMember)
        {
            try
            {
                Boolean flag = false;


                String fullName = "";
                TreeMemBer tree = new TreeMemBer();
                String leftCode = "";
                String rigthCode = "";
                String nhanhLeft = "";
                String nhanhRigth = "";
                Button6.Visible = true;
                Button7.Visible = true;

                if (tree.getMemberFromCode(codeMember).Rows.Count == 0)
                {
                    lblMesses.Text = "Không tồn tại mã này";
                    return;
                }
                DataTable dt = tree.getTreeFromPoint(codeMember, 3);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                    {
                        lblMember.ForeColor = System.Drawing.Color.Red;
                        lblID.ForeColor = System.Drawing.Color.Red;
                    }
                    nhanhLeft = dt.Rows[0]["LeftCode"].ToString().Trim();
                    nhanhRigth = dt.Rows[0]["RigthCode"].ToString().Trim();
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    lblMember.Text = "Tên: "+ fullName + "<br/>&nbsp; ID :";
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label1.Text = "Ngày đăng ký : " + CreatedOn;
                    lblID.Visible = true;
                    lblID.Text = codeMember;
                }
                //nhanh 1
                dt = tree.getMemberFromCode(nhanhLeft);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                    {
                        lblView1.ForeColor = System.Drawing.Color.Red;
                        btnTop.ForeColor = System.Drawing.Color.Red;
                    }
                    rigthCode = dt.Rows[0]["RigthCode"].ToString().Trim();
                    leftCode = dt.Rows[0]["LeftCode"].ToString().Trim();
                    btnTop.Text = dt.Rows[0]["codeMember"].ToString().Trim();
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    lblView1.Text = "Tên: " + fullName + "<br/>&nbsp; ID :";
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label2.Text = "Ngày đăng ký : " + CreatedOn;
                    btnTop.Visible = true;
                }
                else
                {
                    btnTop.Visible = false;
                    lblView1.Text = "Chưa có Member";
                    Label2.Text = "";
                }
                dt = tree.getMemberFromCode(leftCode);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                    {
                        lblView3.ForeColor = System.Drawing.Color.Red;
                        Button1.ForeColor = System.Drawing.Color.Red;
                    }
                    Button1.Text = dt.Rows[0]["codeMember"].ToString().Trim();
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    lblView3.Text = "Tên: " + fullName + "<br/>&nbsp; ID :";
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label4.Text = "Ngày đăng ký : " + CreatedOn;
                    Button1.Visible = true;
                }
                else
                {
                    lblView3.Text = "Chưa có Member";
                    Button1.Visible = false;
                    Label4.Text = "";
                }
                dt = tree.getMemberFromCode(rigthCode);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                    {
                        lblView4.ForeColor = System.Drawing.Color.Red;
                        Button2.ForeColor = System.Drawing.Color.Red;
                    }
                    Button2.Text = dt.Rows[0]["codeMember"].ToString().Trim();
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    lblView4.Text = "Tên: " + fullName + "<br/>&nbsp; ID :";
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label5.Text = "Ngày đăng ký : " + CreatedOn;
                    Button2.Visible = true;
                }
                else
                {
                    lblView4.Text = "Chưa có Member";
                    Button2.Visible = false;
                    Label5.Text = "";
                }
                leftCode = "";
                rigthCode = "";
                //nhanh 2
                dt = tree.getMemberFromCode(nhanhRigth);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                    {
                        lblView2.ForeColor = System.Drawing.Color.Red;
                        Button5.ForeColor = System.Drawing.Color.Red;
                    }
                    rigthCode = dt.Rows[0]["RigthCode"].ToString().Trim();
                    leftCode = dt.Rows[0]["LeftCode"].ToString().Trim();
                    Button5.Text = dt.Rows[0]["codeMember"].ToString().Trim();
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    lblView2.Text = "Tên: " + fullName + "<br/>&nbsp; ID :";
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label3.Text = "Ngày đăng ký : " + CreatedOn;
                    Button5.Visible = true;
                }
                else
                {
                    lblView2.Text = "Chưa có Member";
                    Button5.Visible = false;
                    Label3.Text = "";
                }

                dt = tree.getMemberFromCode(leftCode);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                    {
                        lblView5.ForeColor = System.Drawing.Color.Red;
                        Button3.ForeColor = System.Drawing.Color.Red;
                    }
                    fullName = dt.Rows[0]["fullName"].ToString().Trim();
                    lblView5.Text = "Tên: " + fullName + "<br/>&nbsp; ID :";
                    Button3.Text = dt.Rows[0]["codeMember"].ToString().Trim();
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label6.Text = "Ngày đăng ký : " + CreatedOn;
                    Button3.Visible = true;
                }
                else
                {
                    lblView5.Text = "Chưa có Member";
                    Button3.Visible = false;
                    Label6.Text = "";
                }

                dt = tree.getMemberFromCode(rigthCode);
                if (dt.Rows.Count > 0)
                {
                    flag = Boolean.Parse(dt.Rows[0]["upgrade"].ToString().Trim());
                    if (flag)
                        lblView6.ForeColor = System.Drawing.Color.Red;
                    fullName = dt.Rows[0]["fullName"].ToString().Trim();
                    lblView6.Text = "Tên: " + fullName + "<br/>&nbsp; ID :";
                    fullName = dt.Rows[0]["FullName"].ToString().Trim();
                    Button4.Text = dt.Rows[0]["codeMember"].ToString().Trim();
                    DateTime dateCreate = DateTime.Parse(dt.Rows[0]["CreatedOn"].ToString().Trim());
                    String CreatedOn = dateCreate.Day.ToString() + "-" + dateCreate.Month.ToString() + "-" + dateCreate.Year.ToString();
                    Label7.Text = "Ngày đăng ký : " + CreatedOn;
                    Button4.Visible = true;
                }
                else
                {
                    lblView6.Text = "Chưa có Member";
                    Button4.Visible = false;
                    Label7.Text = "";
                }

                //// ve bang tong hop DealXu va gold
                //dt = tree.getMemberFromCode(codeMember);
                //int week =1;

                //DataTable dtLeft = tree.getSumId(dt.Rows[0]["leftCode"].ToString().Trim());
                //DataTable dtRigth = tree.getSumId(dt.Rows[0]["rigthCode"].ToString().Trim());
                //DataTable dtGoldLeft = tree.getGoldDealXu(dt.Rows[0]["leftCode"].ToString().Trim(), 1);
                //DataTable dtGoldRigth = tree.getGoldDealXu(dt.Rows[0]["rigthCode"].ToString().Trim(), 1);
                //if (dtLeft.Rows.Count > 0 && dtGoldLeft.Rows.Count>0 )
                //{
                //    lblIdLeft.Text = dtLeft.Rows[0]["sumId"].ToString().Trim();

                //    lblGoldLeft.Text = dtGoldLeft.Rows[0]["sumGold"].ToString().Trim();
                //    if (lblDealLeft.Text.Trim() == "")
                //        lblDealLeft.Text = "0";
                //    lblDealLeft.Text = dtGoldLeft.Rows[0]["sumdealxu"].ToString().Trim();
                //    if (lblDealLeft.Text.Trim() == "")
                //        lblDealLeft.Text = "0";
                //}
                //else
                //{
                //    lblDealLeft.Text = "0";
                //    lblGoldLeft.Text = "0";
                //    lblIdLeft.Text = "0";
                //}

                //if (dtRigth.Rows.Count > 0 && dtGoldRigth.Rows.Count > 0)
                //{
                //    lblIdRigth.Text = dtRigth.Rows[0]["sumId"].ToString().Trim();
                //    lblGoldRigth.Text = dtGoldRigth.Rows[0]["sumGold"].ToString().Trim();
                //    if (lblGoldRigth.Text.Trim() == "")
                //        lblGoldRigth.Text = "0";
                //    lblDealRight.Text = dtGoldRigth.Rows[0]["sumdealxu"].ToString().Trim();
                //    if (lblDealRight.Text.Trim() == "")
                //        lblDealRight.Text = "0";
                //}
                //else
                //{
                //    lblDealRight.Text = "0";
                //    lblIdRigth.Text = "0";
                //    lblGoldRigth.Text = "0";
                //}

                //int idLeft = 0;
                //int idRigth = 0;
                //int goldLeft = 0;
                //int goldRigth = 0;
                //int dealLeft = 0;
                //int dealRigth = 0;

                //if (lblIdLeft.Text.Trim() != "")
                //    idLeft = Int32.Parse(lblIdLeft.Text.Trim());
                //if (lblIdRigth.Text.Trim() != "")
                //    idRigth = Int32.Parse(lblIdRigth.Text.Trim());

                //if (lblGoldLeft.Text.Trim() != "")
                //    goldLeft = Int32.Parse(lblGoldLeft.Text.Trim());
                //if (lblIdRigth.Text.Trim() != "")
                //    goldRigth = Int32.Parse(lblGoldRigth.Text.Trim());

                //if (lblGoldLeft.Text.Trim() != "")
                //    dealLeft = Int32.Parse(lblDealLeft.Text.Trim());
                //if (lblIdRigth.Text.Trim() != "")
                //    dealRigth = Int32.Parse(lblDealRight.Text.Trim());


                //lblIdSum.Text = (idLeft + idRigth).ToString();
                //lblGoldSum.Text = (goldLeft + goldRigth).ToString();
                //lblDealSum.Text = (dealLeft + dealRigth).ToString();
                flag = true;
            }
            catch (Exception ee)
            {
                lblMesses.Text = "Error: " + ee.Message;
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            TreeMemBer tree = new TreeMemBer();
            if (tree.checkExists(info.CodeMember, txtCodeMember.Text.Trim()))
            {

                //String url = "MaTrix.aspx?codeMember=" + txtCodeMember.Text.Trim();
                //Response.Redirect(url);
                drawTree(txtCodeMember.Text.Trim());
            }
            else
            {
                lblMesses.Text = "Không tồn tại mã này";
            }
            txtCodeMember.Text = "";
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            String lastMember = "";
            TreeMemBer tree = new TreeMemBer();
            DataTable dt = tree.getMemberLast(Button5.Text.Trim());
            if (dt.Rows.Count > 0)
            {
                lastMember = dt.Rows[0]["CodeMember"].ToString().Trim();
                drawTree(lastMember);
                //String url = "MaTrix.aspx?codeMember=" + lastMember;
                //Response.Redirect(url);
            }
        }
    }
}